/*
 * import.c
 *
 *  Created on: 2013-5-13
 *      Author: xkaying
 */


#include "import.h"
#include "../common/list.h"
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>


struct _import{
		List *packages;
};

Import *ImportInit()
{
	Import *import;
	import=(Import*)malloc(sizeof(Import));
	import->packages=ListInit();
	return import;
}

void ImportFree(Import *import)
{
	if(import){
		if(import->packages){
			void *p;
			p=ListPop(import->packages);
			while(p){
				p=ListPop(import->packages);
			}
			free(import->packages);
		}
		free(import);
	}
}

int ImportCompare(Import *psrc,Import *pdest)
{
	assert(psrc);
	assert(pdest);

	if(ListLength(psrc->packages)==ListLength(pdest->packages)){
		//比较每个字符串
		ListCursor *psrccursor;
		ListCursor *pdestcursor;
		psrccursor=ListGetCursor(psrc->packages);
		pdestcursor=ListGetCursor(pdest->packages);
		char *psrcstr;
		char *pdeststr;
		psrcstr=ListForeach(psrc->packages,psrccursor);
		pdeststr=ListForeach(pdest->packages,pdestcursor);
		while(psrcstr){
			if(strcmp(psrcstr,pdeststr)){
				return 1;
			}
		}
		return 0;
	}
	return 1;

}

void ImportPrint(Import *import,FILE *fl)
{
	assert(import);
	assert(import);
	char *data;
	ListCursor *pcursor;
	pcursor=ListGetCursor(import->packages);
	data=(char*)ListForeach(import->packages,pcursor);
	while(data){
		fprintf(fl,"%s");
		data=(char*)ListForeach(import->packages,pcursor);
		if(data){
			fprintf(fl,".");
		}
	}

}

void ImportAppend(Import *import,char *name)
{
	assert(import);
	assert(name);
	ListPush(import->packages,name);
}
